package com.bo.redis.redisbase.basetypes;

import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

/**
 * @Author: gpb
 * @Date: 2025/1/22 22:37
 * @Description:
 */
@Component
public class RedisString {
    private static final String REDIS_STRING_KEY = "rs:";

    @Resource
    private JedisPool jedisPool;

    /** resource
     * 向Redis中存值，永久有效
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.set(REDIS_STRING_KEY + key, value);
        } catch (Exception e) {
            throw new RuntimeException("向Redis中存值失败！");
        } finally {
            jedis.close();
        }
    }

    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(REDIS_STRING_KEY + key);
        } catch (Exception e) {
            throw new RuntimeException("从Redis中取值失败！");
        } finally {
            jedis.close();
        }
    }
}
